$(function () {
    getUnzip();
    gotoTop();
})
var prefix = "/home/productBrowsing"
function getUnzip() {
	var ncFileId = $("#ncFileId").val();
	var tbody = $("#tbody");
	$.ajax({
        type: "post",
        async: true,
        url: prefix+"/getUnzip",
        data: {
            "ncFileId": ncFileId,
        },
        dataType: "json",        //返回数据形式为json
        success: function (res) {
            tbody.empty();
            result = res.data;
            for (var i = 0; i < result.length; i++) {
                tbody.append('<tr>');
				tbody.append('<td class="prt_tab_1">' + result[i].fileName + '</td>');
                var arr=[".nc",".word",".ppt",".pptx",".txt",".excel"];
                var a = arr.indexOf(getType(result[i].fileName));
                if("-1" == a ){
                    tbody.append('<td class="prt_tab_1">' + result[i].fileSize + 'MB</td>' +
                        '<td class="prt_tab_1">' + paraseDate(result[i].updateDate) + '</td>' +
                        '<td class="prt_tab_2" width="180px"><a href="javascript:downLoad(\'' + result[i].id + '\')">下载</a>&nbsp;&nbsp;' +
                        '<a href="javascript:toOrderDownLoad(\'' + result[i].id + '\')">下载器下载</a>&nbsp;&nbsp;' +
                        '</td></tr>');
                }else {
                    tbody.append('<td class="prt_tab_1">' + result[i].fileSize + 'MB</td>' +
                        '<td class="prt_tab_1">' + paraseDate(result[i].updateDate) + '</td>' +
                        '<td class="prt_tab_2" width="180px"><a href="javascript:downLoad(\'' + result[i].id + '\')">下载</a>&nbsp;&nbsp;' +
                        '<a href="javascript:toOrderDownLoad(\'' + result[i].id + '\')">下载器下载</a>&nbsp;&nbsp;' +
                        '<a href="javascript:isPreview(\'' + result[i].productId + ','+ result[i].id +','+ result[i].fileName +','+  paraseDate(result[i].updateDate) + ',' + result[i].filePath +'\')">&nbsp;预览</a>' +
                        '</td></tr>');
                }
            }
        }
    });
}
//获取文件后缀
function getType(file){
    var filename=file;
    var index1=filename.lastIndexOf(".");
    var index2=filename.length;
    var type=filename.substring(index1,index2);
    return type;
}
function downLoad(unzipId) {
    if (!isLogin()) {
        showLoginDiv();
    } else {
        window.location.href = prefix+"/downloadUnzip?unzipId=" + unzipId;
    }
}
function isLogin() {
    var loginName = "[[${@permission.getPrincipalPropertyUnLogged('loginName')}]]";
    if (loginName){
        return true
    }else {
        return false
    }
}

function isStrInArray(str, arr) {
    var n = arr.length;
    for (var i = 0; i < n; i++) {
        if (arr[i] == str) {
            return true;
        }
    }
    return false;
}
function isPreview(params) {
    let result = params.split(",");
    $.ajax({
        url: prefix+"/isPreview",
        type: "post",
        async: true,
        data: {
            "productId": result[0],
        },
        dataType: "json",
        success: function (data) {
            console.log(data)
            console.log("----------")
            if (data.success) {
                window.location.href = prefix+"/toPreviews?productId=" + result[0] + "&id=" + result[1] +
                    "&fileName=" + result[2] + "&updateDate=" + result[3];
            } else {
                //doc、docx、ppt、pptx、xls、xlsx、txt、pdf
                alert("仅支持 word、ppt、txt、excel 等格式预览");
            }
        }
    });
}
function toOrderDownLoad(unzipId) {
    if (!isLogin()) {
        showLoginDiv();
    } else {
    	$.ajax({
    		type: "post",
            async: true,
            url: "introduce.do?unzipTransferToDownOrder",
            data: {
                "unzipId": unzipId
            },
            success: function (data) {
            	var id = eval(data);
            	if (id == '1') {
					alert("已添加至下载器");
				} else if (id == '2') {
					alert("该文件尚未下载完成，请下载完成后再添加");
				} else if (id == '3'){
					alert("部门未下载文件达到上限，不能添加，请先下载未下载的文件再进行添加");
				} else {
					alert("下载器添加文件失败");
				}
            }
    	});
    }
}

function paraseDate(date) {
    if (date == undefined) {
        return "未知";
    }
    var y = date.year < 1900 ? 1900 + date.year : date.year;
    var m = date.month + 1;
    m = m < 10 ? '0' + m : m;
    var d = date.date;
    d = d < 10 ? ('0' + d) : d;
    var h = date.hours;
    h = h < 10 ? ('0' + h) : h;
    var min = date.minutes;
    min = min < 10 ? ('0' + min) : min;
    var s = date.seconds;
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s;
}

